home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / solar6.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.0 KB  |  239 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* solar6.c - a simple solar system.  
  19.  *    Added a reshape handler to reset the viewport and viewing volume.
  20.  *
  21.  *    F1 key            - print help information
  22.  *    SPACE Key        - toggle between solid/wireframe models
  23.  *    Escape Key        - exit program
  24.  */
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28.  
  29. #include <stdio.h>
  30. #include <math.h>
  31.  
  32. /* Function Prototypes */
  33.  
  34. GLvoid initgfx( GLvoid );
  35. GLvoid keyboard( GLubyte, GLint, GLint );
  36. GLvoid specialkeys( GLint, GLint, GLint );
  37. GLvoid reshape( GLsizei, GLsizei );
  38. GLvoid drawScene( GLvoid );
  39.  
  40. void checkError( char * );
  41. void printHelp( char * );
  42.  
  43. /* Global Variables */
  44.  
  45. static char *progname; 
  46.  
  47. static GLboolean filledFlag = GL_TRUE;
  48.  
  49. /* Global Definitions */
  50.  
  51. #define KEY_ESC    27    /* ascii value for the escape key */
  52.  
  53. void
  54. main( int argc, char *argv[] )
  55. {
  56.     GLsizei width, height;
  57.  
  58.     glutInit( &argc, argv );
  59.  
  60.     /* create a window that is 1/4 the size of the screen,
  61.      * and position it in the middle of the screen.
  62.      */
  63.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  64.     height = glutGet( GLUT_SCREEN_HEIGHT );
  65.     glutInitWindowPosition( width / 4, height / 4 );
  66.     glutInitWindowSize( width / 2, height / 2 );
  67.     glutInitDisplayMode( GLUT_RGBA );
  68.     glutCreateWindow( argv[0] );
  69.  
  70.     initgfx();
  71.  
  72.     glutReshapeFunc( reshape );
  73.     glutKeyboardFunc( keyboard );
  74.     glutSpecialFunc( specialkeys );
  75.     glutDisplayFunc( drawScene ); 
  76.  
  77.     progname = argv[0];
  78.  
  79.     printHelp( progname );
  80.  
  81.     glutMainLoop();
  82. }
  83.  
  84. void
  85. printHelp( char *progname )
  86. {
  87.     fprintf(stdout, 
  88.         "\n%s - model some objects\n\n"
  89.         "F1 key        - print help information\n"
  90.         "SPACE key    - toggle between solid/wireframe mode\n"
  91.         "Escape Key    - exit the program\n\n",
  92.         progname);
  93. }
  94.  
  95. GLvoid
  96. initgfx( GLvoid )
  97. {
  98.     /* set clear color to black */
  99.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  100. }
  101.  
  102. GLvoid 
  103. reshape( GLsizei width, GLsizei height )
  104. {
  105.     GLdouble    aspect;
  106.  
  107.     glViewport( 0, 0, width, height );
  108.  
  109.     /* compute aspect ratio */
  110.     aspect = (GLdouble) width / (GLdouble) height;
  111.  
  112.     glMatrixMode( GL_PROJECTION );
  113.  
  114.     /* Reset world coordinates first ... */
  115.     glLoadIdentity();
  116.  
  117.     /* Reset the viewing volume based on the new aspect ratio */
  118.     gluPerspective( 45.0, aspect, 1.0, 20.0 );
  119.  
  120.     glMatrixMode( GL_MODELVIEW );
  121. }
  122.  
  123. void 
  124. checkError( char *label )
  125. {
  126.     GLenum error;
  127.     while ( (error = glGetError()) != GL_NO_ERROR )
  128.         printf( "%s: %s\n", label, gluErrorString(error) );
  129. }
  130.  
  131. GLvoid 
  132. keyboard( GLubyte key, GLint x, GLint y )
  133. {
  134.     switch (key) {
  135.     case ' ':    /* toggle fill mode */
  136.         filledFlag = !filledFlag;
  137.         glutPostRedisplay();
  138.         break;
  139.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  140.         exit(0);
  141.     }
  142. }
  143.  
  144. GLvoid 
  145. specialkeys( GLint key, GLint x, GLint y )
  146. {
  147.     switch (key) {
  148.     case GLUT_KEY_F1:    /* Function key #1 */
  149.         /* print help information */
  150.         printHelp( progname );
  151.         break;
  152.     }
  153. }
  154.  
  155. GLvoid
  156. drawScene( GLvoid )
  157. {
  158.     static GLfloat    year = 45.0, day = -90.0;
  159.  
  160.     static GLfloat  yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  161.     static GLfloat  blue[] = { 0.0, 0.0, 1.0, 1.0 };
  162.     static GLfloat  gray[] = { 0.2, 0.2, 0.2, 1.0 };
  163.  
  164.     glClear( GL_COLOR_BUFFER_BIT );
  165.  
  166.     glPushMatrix();
  167.  
  168.         /* move into the viewing volume */
  169.         glTranslatef( 0.0, 0.0, -10.0 );
  170.  
  171.         /* draw sun */
  172.  
  173.         glPushMatrix();
  174.  
  175.             /* Give the sun a yellow glow */
  176.             glColor4fv( yellow );
  177.           
  178.             /* rotate on our own axis */
  179.             glRotatef( 90.0, 1.0, 0.0, 0.0 );
  180.             if (filledFlag)
  181.                 glutSolidSphere( 0.7, 15, 15 );
  182.             else
  183.                 glutWireSphere( 0.7, 15, 15 );
  184.  
  185.         glPopMatrix();
  186.  
  187.         glPushMatrix();
  188.  
  189.             /* draw earth */
  190.  
  191.             /* rotate to the right time of year */
  192.             glRotatef( year, 0.0, 1.0, 0.0 );
  193.  
  194.             /* translate out to our orbit about the sun */
  195.             glTranslatef( 3.5, 0.0, 0.0 );
  196.             glPushMatrix();
  197.  
  198.                 /* set color for the earth */
  199.                 glColor4fv( blue );
  200.  
  201.                 /* rotate on our own axis */
  202.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  203.                 if (filledFlag)
  204.                     glutSolidSphere( 0.4, 15, 15 );
  205.                 else
  206.                     glutWireSphere( 0.4, 15, 15 );
  207.                 
  208.             glPopMatrix();
  209.  
  210.             /* draw moon */
  211.             glPushMatrix();
  212.  
  213.                 /* set color for the moon */
  214.                 glColor4fv( gray );
  215.  
  216.                 /* rotate to the right time of day */
  217.                 glRotatef( day, 0.0, 1.0, 0.0 );
  218.  
  219.                 /* translate out to our orbit about the earth */
  220.                 glTranslatef( 1.0, 0.0, 0.0 );
  221.  
  222.                 /* rotate on our axis */
  223.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  224.                 if (filledFlag)
  225.                     glutSolidSphere( 0.2, 15, 15 );
  226.                 else
  227.                     glutWireSphere( 0.2, 15, 15 );
  228.  
  229.             glPopMatrix();
  230.  
  231.         glPopMatrix();
  232.  
  233.     glPopMatrix();
  234.  
  235.     checkError( "drawScene" );
  236.  
  237.     glFlush();
  238. }
  239.